﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
namespace ProjectSem3.Admin
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        private DataClassesHeaxlthDataContext dc = new DataClassesHeaxlthDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                AdminLogin ad = (AdminLogin)Session["admin"];
                if (ad == null)
                {
                    Response.Redirect("login.aspx");
                }
                else
                {
                    if (ad.pemision == 2)
                    {
                        Response.Redirect("login.aspx");
                    }

                }
            }
         }

        protected void ButtonInsert_Click(object sender, EventArgs e)
        {
            var user = this.dc.AdminLogins.Where(a => a.username.Equals(this.txtusername.Value)).ToList();
            if (user.Count > 0)
            {
                this.Labelkq.Text = "Username already exists";
            }
            else
            {
                string username = this.txtusername.Value;
                string password = mahoa_giaima.maHoa(this.txtpassword.Value);
                string retype = mahoa_giaima.maHoa(this.retype.Value);
                int pemision = Convert.ToInt32(this.pemision.Value);
                themtk(username, password, retype, pemision);
            }
        }

        public void themtk(string username, string password, string retype, int pemision)
        {
            AdminLogin ad = new AdminLogin();
            ad.username = username;
            ad.password = password;
            ad.pemision = pemision;
            if (this.txtpassword.Value != this.retype.Value)
            {
                this.Labelkq.Text = "The passwords you entered do not match.";
            }
            else
            {
                try
                {
                    this.dc.AdminLogins.InsertOnSubmit(ad);
                    this.dc.SubmitChanges();
                    this.Labelkq.Text = "Insert successful";
                    Response.Redirect("accountadmin.aspx");
                }
                catch
                {
                    this.Labelkq.Text = "Insert failed";
                }
            }
        }
    }
    
}